//
//  HTTPTOOL.m
//  行记
//
//  Created by dllo on 15/9/22.
//  Copyright (c) 2015年 lhch. All rights reserved.
//

#import "HTTPTOOL.h"


@implementation HTTPTOOL

+ (void)GETWithURL:(NSString *)url body:(NSDictionary *)body httpHead:(NSDictionary *)head responseStyle:(responseStyle)style success:(void (^)(id))success fail:(void (^)(NSError *))fail {
    
    /* 1.创建一个网络请求管理者 */
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    /* 2.添加请求头 */
    if (head) {
        for (NSString *key in head) {
            [manager.requestSerializer setValue:head[key] forHTTPHeaderField:key];
        }
    }
    
    /* 3.判断返回数据类型 */
    switch (style) {
        case DATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case JSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    /* 4.设置请求接受的数据类型 */
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    /* 5.GET请求 */
    [manager GET:url parameters:body success:^void(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^void(AFHTTPRequestOperation * operation, NSError * error) {
        if (fail) {
            fail(error);
        }
    }];
}

+ (void)POSTWithURL:(NSString *)url body:(id)body bodyStyle:(bodyStyle)bodyStyle httpHead:(NSDictionary *)head responseStyle:(responseStyle)style success:(void (^)(id))success fail:(void (^)(NSError *))fail {
    
    /* 1.创建 HTTP请求管理者 */
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    /* 2.处理 body 类型 */
    switch (bodyStyle) {
        case JSONStyle:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case stringStyle:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError **error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    
    /* 3.设置请求接受的数据类型 */
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    /* 4.判断返回数据类型 */
    switch (style) {
        case DATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case JSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        default:
            break;
    }
    
    /* 5.添加请求头 */
    if(head) {
        for (NSString *key in head) {
            [manager.requestSerializer setValue:head[key] forHTTPHeaderField:key];
        }
    }
    
    /* 6.POST 请求 */
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
    
}

@end
